import '../model/single_chat_model.dart';

/// 单聊默认数据
/// 围绕admin用户创建默认的单聊数据
class SingleChatData {
  /// 获取默认单聊数据
  static List<SingleChatModel> getDefaultSingleChats() {
    final now = DateTime.now();

    return [
      // Admin与GardenMaster的聊天
      SingleChatModel(
        id: 'single_chat_1',
        userIds: ['1', '2'], // admin, gardenmaster
        createTime: now.subtract(const Duration(days: 5)),
        lastMessageTime: now.subtract(const Duration(hours: 2)),
        lastMessage: 'Thanks for the watering tips!',
        lastMessageSenderId: '2',
      ),

      // Admin与LeafyFriend的聊天
      SingleChatModel(
        id: 'single_chat_2',
        userIds: ['1', '3'], // admin, leafyfriend
        createTime: now.subtract(const Duration(days: 3)),
        lastMessageTime: now.subtract(const Duration(minutes: 30)),
        lastMessage: 'How is your new plant doing?',
        lastMessageSenderId: '1',
      ),

      // Admin与PlantCare的聊天
      SingleChatModel(
        id: 'single_chat_3',
        userIds: ['1', '4'], // admin, plantcare
        createTime: now.subtract(const Duration(days: 7)),
        lastMessageTime: now.subtract(const Duration(hours: 1)),
        lastMessage: 'I\'ll send you some photos tomorrow',
        lastMessageSenderId: '4',
      ),

      // Admin与GreenSoul的聊天
      SingleChatModel(
        id: 'single_chat_4',
        userIds: ['1', '5'], // admin, greensoul
        createTime: now.subtract(const Duration(days: 2)),
        lastMessageTime: now.subtract(const Duration(days: 1)),
        lastMessage: 'The fertilizer worked great!',
        lastMessageSenderId: '5',
      ),

      // Admin与Blooming的聊天
      SingleChatModel(
        id: 'single_chat_5',
        userIds: ['1', '6'], // admin, blooming
        createTime: now.subtract(const Duration(days: 4)),
        lastMessageTime: now.subtract(const Duration(hours: 6)),
        lastMessage: 'Can you help me with pruning?',
        lastMessageSenderId: '1',
      ),

      // Admin与PlantLover的聊天
      SingleChatModel(
        id: 'single_chat_6',
        userIds: ['1', '7'], // admin, plantlover
        createTime: now.subtract(const Duration(days: 1)),
        lastMessageTime: now.subtract(const Duration(minutes: 15)),
        lastMessage: 'Sure, I\'ll be there at 3 PM',
        lastMessageSenderId: '7',
      ),

      // Admin与GardenNewbie的聊天
      SingleChatModel(
        id: 'single_chat_7',
        userIds: ['1', '8'], // admin, gardennewbie
        createTime: now.subtract(const Duration(hours: 12)),
        lastMessageTime: now.subtract(const Duration(minutes: 5)),
        lastMessage: 'Welcome to our plant community!',
        lastMessageSenderId: '1',
      ),
    ];
  }

  /// 获取指定用户的聊天列表
  static List<SingleChatModel> getChatsByUserId(String userId) {
    return getDefaultSingleChats()
        .where((chat) => chat.isUserInChat(userId))
        .toList();
  }

  /// 获取两个用户之间的聊天
  static SingleChatModel? getChatBetweenUsers(String userId1, String userId2) {
    try {
      return getDefaultSingleChats().firstWhere(
        (chat) => chat.isUserInChat(userId1) && chat.isUserInChat(userId2),
      );
    } catch (e) {
      return null;
    }
  }

  /// 获取聊天统计信息
  static Map<String, dynamic> getChatStatistics() {
    final chats = getDefaultSingleChats();
    final stats = <String, dynamic>{};

    // 总聊天数
    stats['totalChats'] = chats.length;

    // 活跃聊天数（有最后消息的）
    stats['activeChats'] =
        chats.where((chat) => chat.lastMessage != null).length;

    // 最近活跃的聊天数（24小时内）
    final now = DateTime.now();
    final oneDayAgo = now.subtract(const Duration(days: 1));
    stats['recentActiveChats'] =
        chats.where((chat) {
          final lastMessageTime = chat.lastMessageTime ?? chat.createTime;
          return lastMessageTime.isAfter(oneDayAgo);
        }).length;

    // 按用户统计聊天数
    final userChatCounts = <String, int>{};
    for (final chat in chats) {
      for (final userId in chat.userIds) {
        userChatCounts[userId] = (userChatCounts[userId] ?? 0) + 1;
      }
    }
    stats['userChatCounts'] = userChatCounts;

    return stats;
  }

  /// 获取最活跃的聊天
  static List<SingleChatModel> getMostActiveChats({int limit = 5}) {
    final chats = getDefaultSingleChats();
    chats.sort((a, b) {
      final aTime = a.lastMessageTime ?? a.createTime;
      final bTime = b.lastMessageTime ?? b.createTime;
      return bTime.compareTo(aTime);
    });
    return chats.take(limit).toList();
  }

  /// 获取最新创建的聊天
  static List<SingleChatModel> getLatestChats({int limit = 5}) {
    final chats = getDefaultSingleChats();
    chats.sort((a, b) => b.createTime.compareTo(a.createTime));
    return chats.take(limit).toList();
  }
}
